package com.cn.sl.server.api.system.department.controller

import cn.dev33.satoken.annotation.SaCheckLogin
import com.cn.sl.server.api.system.department.model.dto.AddDepartmentParam
import com.cn.sl.server.api.system.department.model.dto.DepartmentDto
import com.cn.sl.server.api.system.department.model.dto.EditDepartmentParam
import com.cn.sl.server.api.system.department.service.DepartmentService
import com.cn.sl.server.common.constant.Constant
import com.cn.sl.server.util.ResponseMessageDto
import com.cn.sl.server.util.deleteSuccessMsg
import com.cn.sl.server.util.ok
import com.cn.sl.server.util.saveSuccessMsg
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 部门管理
 *
 * @author 阮雪峰
 */
@Tag(name = "部门管理")
@SaCheckLogin
@RestController
@RequestMapping("${Constant.ADMIN_ROOT}/department")
class DepartmentController(
    val departmentService: DepartmentService
) {
    /**
     * 列表查询所有
     *
     * @author 阮雪峰
     */
    @Operation(summary = "列表查询所有")
    @PostMapping("/find-all")
    fun findAll(): ResponseEntity<List<DepartmentDto>> {
        return ok(departmentService.findAll())
    }

    /**
     * 新增
     *
     * @author 阮雪峰
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    fun add(@RequestBody @Validated param: AddDepartmentParam): ResponseEntity<ResponseMessageDto> {
        departmentService.add(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 编辑
     *
     * @author 阮雪峰
     */
    @Operation(summary = "编辑")
    @PostMapping("/edit")
    fun edit(@RequestBody @Validated param: EditDepartmentParam): ResponseEntity<ResponseMessageDto> {
        departmentService.edit(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "根据id查询")
    @GetMapping("/find/{id}")
    fun findById(@PathVariable id: Long): ResponseEntity<DepartmentDto> {
        return ok(departmentService.findById(id))
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "根据id删除")
    @DeleteMapping("/delete/{id}")
    fun delete(@PathVariable id: Long): ResponseEntity<ResponseMessageDto> {
        departmentService.delete(id)
        return deleteSuccessMsg()
    }
}